package LeetCode.leetcode;

/**
 * 最大子序和
 *
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/6
 * @time: 21:02
 */
public class Test53最大子序和 {
    public static void main(String[] args) {
        int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(maxSubArray(nums));
    }
    public static int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            throw new RuntimeException("输入非法");
        }
        int max = Integer.MIN_VALUE;

        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if (sum > max) {
                max = sum;
            }
            if (sum < 0) {
                sum = 0;
            }
        }
        return max;
    }

}

